const sqlTool = require('../tool/mysql')

const queryAreaCode = (code) => {
  return new Promise((resolve, reject) => {
    if (!code) {
      reject('编码为空')
      return
    }
    const sql = `select area_code, lng, lat from address_gps where area_code = ${code} limit 1;`
    const connection = sqlTool.connection
    connection.query(sql, (err, res) => {
      if (err) {
        console.log(err)
        reject(err)
      }
      resolve(res)
      // connection.end()
    })
  })
}
const addAreaCode = (innserVal) => {
  return new Promise((resolve, reject) => {
    if (!innserVal) {
      reject('编码为空')
      return
    }
    const sql = `insert into address_gps (area_code, area_name, lng, lat, comprehension, level) values ('${innserVal.area_code}', '${innserVal.area_name}', '${innserVal.lng}', '${innserVal.lat}', '${innserVal.comprehension}', '${innserVal.level}');`
    console.log(sql)
    const connection = sqlTool.connection
    connection.query(sql, (err, res) => {
      if (err) {
        console.log(err)
        reject(err)
      }
      resolve(res)
      // connection.end()
    })
  })
}

module.exports = {
  queryAreaCode,
  addAreaCode
}
